package mil.af.amc.adpetracker.web.actions.struts1.dra;

import mil.af.amc.adpetracker.model.DRA;
import mil.af.amc.adpetracker.service.DraService;
import mil.af.amc.adpetracker.service.exceptions.DraExistsException;
import mil.af.amc.adpetracker.service.exceptions.DraNotFoundException;
import mil.af.amc.adpetracker.service.exceptions.DraAccountsStillAssignedException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractDispatchAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class DraDispatchAction extends AbstractDispatchAction {

    private Log log = LogFactory.getLog(DraDispatchAction.class);

    private DraService draService;

    public void setDraService(DraService draService) {
        this.draService = draService;
    }

    public ActionForward viewDraList(ActionMapping actionMapping,
                                     ActionForm actionForm,
                                     HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("success");
    }

    public ActionForward viewAddDra(ActionMapping actionMapping,
                                    ActionForm actionForm,
                                    HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAddDra");

    }

    public ActionForward addDra(ActionMapping actionMapping,
                                ActionForm actionForm,
                                HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse) {

        DynaActionForm draForm = (DynaActionForm) actionForm;
        ActionMessages errors = draForm.validate(actionMapping, httpServletRequest);

        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("failureAddDra");
        }

        DRA dra;
        try {
            dra = draService.addDra(draForm.getMap());
        } catch (DraExistsException e) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.dra.alreadyexists", draForm.getMap().get("draName")));
            return actionMapping.findForward("failureAddDra");
        }
        if (dra == null) {
            Integer draId = (Integer) draForm.getMap().get("draId");
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.dra.notfound", draId));
            return actionMapping.findForward("failureAddDra");
        }

        return actionMapping.findForward("successAddDra");

    }

    public ActionForward viewEditDra(ActionMapping actionMapping,
                                     ActionForm actionForm,
                                     HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewEditDra");

    }

    public ActionForward editDra(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) {


        DynaActionForm draForm = (DynaActionForm) actionForm;
        ActionMessages errors = draForm.validate(actionMapping, httpServletRequest);

        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("failureEditDra");
        }

        DRA dra;
        try {
            dra = draService.editDra(draForm.getMap());
        } catch (DraNotFoundException e) {
            Integer draId = (Integer) draForm.getMap().get("draId");
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.dra.notfound", draId));
            return actionMapping.findForward("failureEditDra");
        }

        return actionMapping.findForward("successEditDra");
    }

    public ActionForward deleteDra(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {

        DynaActionForm draForm = (DynaActionForm) actionForm;

        try {
            draService.deleteDra(draForm.getMap());
        } catch (DraNotFoundException e) {
            int draId = (Integer) draForm.get("draId");
            saveError(httpServletRequest, new ActionMessage("error.dra.notfound", draId));
            return actionMapping.findForward("failureDeleteDra");
        } catch (DraAccountsStillAssignedException e) {

            String draName = (String) draForm.get("draName");
            saveError(httpServletRequest, new ActionMessage("error.dra.accountsstillassigned", draName));
            return actionMapping.findForward("failureDeleteDra");

        }

        return actionMapping.findForward("success");

    }

    public ActionForward selectDra(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        int draId = (Integer) form.get("draId");

        httpServletRequest.setAttribute("draId", draId);

        return actionMapping.findForward("viewAdpeAccountIndex");

    }
}
